package v1

import (
	"ginblog/middleware"
	errMsg "ginblog/model/err"
	"ginblog/model/request"
	"ginblog/model/response"
	"ginblog/service"
	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	var (
		data  request.Login
		token string
		code  errMsg.Code
	)
	_ = c.ShouldBindJSON(&data)
	// todo 参数校验
	if code = service.Login(&data); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	if token, code = middleware.SetToken(data.Username); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	response.Ok(c, token)
}
func Upload(c *gin.Context) {
	_, fileHeader, err := c.Request.FormFile("file")
	if err != nil {
		response.Fail(c, errMsg.OSS_UPLOAD_FAIL, nil)
		return
	}
	if fileHeader.Size > 1024*1024*10 {
		response.Fail(c, errMsg.OSS_UPLOAD_FAIL, nil, "超出文件大小限制！")
		return
	}
	uid, code := service.Upload(fileHeader)
	if code != errMsg.SUCCESS {
		response.Fail(c, errMsg.OSS_UPLOAD_FAIL, nil)
		return
	}
	response.Ok(c, gin.H{
		"size": uid,
		//"file":
	})
}
